package com.clouddot.baipai.ui.util.tableview.toolkits;

import android.content.Context;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.widget.GridLayout;
import android.widget.TextView;


import com.clouddot.baipai.R;
import com.clouddot.baipai.infrastructure.environment.Environment;
import com.clouddot.baipai.ui.dialog.A_courseDailog;
import com.clouddot.baipai.ui.util.tableview.TableDataAdapter;
import com.clouddot.baipai.ui.util.tableview.common.TableCellData;

import java.util.List;

/**
 * Created by JKWANG-PC on 2017/2/16.
 */

public final class SimpleTableDataAdapter extends TableDataAdapter {

    private float textSize = 18;
    private int paddingLeft = 20;
    private int paddingTop = 15;
    private int paddingRight = 20;
    private int paddingBottom = 15;
    private int typeface = Typeface.NORMAL;
    private int textColor = 0x99000000;

    public SimpleTableDataAdapter(Context context, List<TableCellData> data, int columnCount) {
        super(context, data, columnCount);
    }

    @Override
    protected void addGridLayoutView(final Context context, List<TableCellData> cellDatas) {

        for (int i = 0, n = cellDatas.size(); i < n; ++i) {
            final TableCellData cellData = cellDatas.get(i);
            TextView view = new TextView(context);
            view.setText(cellData.getValue());

            //设置配置参数
            GridLayout.Spec rowSpec = GridLayout.spec(cellData.getRow(), cellData.getRowSpan());
            GridLayout.Spec colomnSpce = GridLayout.spec(cellData.getColumn(), cellData.getColumnSpan());
            GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec, colomnSpce);
            params.setGravity(Gravity.FILL);
            params.height = Environment.getInstance().Height;
            params.width = (int) (tableDataViewWidth * cellData.getColumnSpan() / columnCount); //默认宽度
            view.setGravity(Gravity.CENTER);
            view.setTextSize(textSize);
            view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
            view.setTypeface(view.getTypeface(), typeface);
            if(cellData.getRow() == 0){
                view.setTextColor(context.getResources().getColor(R.color.red));  //设置日期的红色字体颜色
            }else{
                view.setTextColor(textColor);  //设置日期的默认字体颜色
            }


            //开始画课表
            if(cellData.getColumn() == 0 && cellData.getRow() == 0){
                params.width = 100;
                view.setBackgroundResource(R.drawable.tv_border_odd); //第一格的背景格式
            }else if(cellData.getColumn() == 0){
                params.width = 100;
                view.setBackgroundResource(R.drawable.tv_border_odd2); //第一列的背景格式
            }
            else if (cellData.getRow() > 0) {
                if(cellData.getColumn() == 1){//第二列显示时间的宽度修改
                    params.width = (int)(tableDataViewWidth * cellData.getColumnSpan() / columnCount) * 2 - 100 ;
                }
                view.setBackgroundResource(R.drawable.selector_table);//剩下的背景格式
            } else {
                view.setBackgroundResource(R.drawable.tv_border_first_row);//第一行的背景格式
            }

           //课程点击事件
            if(cellData.getRow() > 0 && cellData.getColumn() > 1 && !cellData.getDayInfo().getCourseName().isEmpty()){
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final A_courseDailog dailog = new A_courseDailog(context);
                        TextView[] textViews = dailog.getTextViews();
                        textViews[0].setText(cellData.getDayInfo().getCourseName());
                        textViews[1].setText("授课教师："+cellData.getDayInfo().getTeacherName());
                        textViews[2].setText("当前周数：第"+cellData.getDayInfo().getWeekHours()+"周");
                        textViews[3].setText("上课周数："+cellData.getDayInfo().getBeginWeek()+" ~ "+cellData.getDayInfo().getEndWeek());
                        textViews[4].setText("课程介绍："+cellData.getDayInfo().getRemark());

                        dailog.showDialog();
                    }
                });
            }

            tableDataView.addView(view, params);
        }
    }


    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public void setPaddings(final int left, final int top, final int right, final int bottom) {
        this.paddingLeft = left;
        this.paddingTop = top;
        this.paddingRight = right;
        this.paddingBottom = bottom;
    }

    public void setPaddingLeft(int paddingLeft) {
        this.paddingLeft = paddingLeft;
    }

    public void setPaddingTop(int paddingTop) {
        this.paddingTop = paddingTop;
    }

    public void setPaddingRight(int paddingRight) {
        this.paddingRight = paddingRight;
    }

    public void setPaddingBottom(int paddingBottom) {
        this.paddingBottom = paddingBottom;
    }

    public void setTypeface(int typeface) {
        this.typeface = typeface;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }
}
